/*
nums = [2,7,11,15], target = 9
[0,1]
因为 nums[0] + nums[1] == 9 ，返回 [0, 1]
*/

const nums = [2, 7, 11, 15]
const target = 9
//暴力解
var twoSum = function(nums, target) {
  const lg = nums.length - 1
  for (let i = 0; i < lg; i++) {
    for (let j = i + 1; j < lg; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j]
      }
    }
  }
  return -1
};

console.log(twoSum(nums, target))

//Map 结构解
var twoSum2 = function(nums, target) {
  const map = new Map()
  const lg = nums.length - 1
  for (let i = 0; i < lg; i++) {
    map.set(nums[i], i)
    let other = target - nums[i]
    if (map.has(other)) {
      return [map.get(other), i]
    }
  }
  return -1
}

console.log(twoSum2(nums, target))

